package cn.wolfcode.luowowo.common.util;

import cn.wolfcode.luowowo.common.exception.LogicException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证手机号是否符合规范
 */
public class PhoneUtil {

    public static void phoneMatch(String phone) {
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        if (phone.length() != 11) {
            throw new RuntimeException("手机号应为11位数");
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if (!isMatch) {
               throw new LogicException(phone+"\t手机号码格式错误");
            }
        }
    }

}
